package com.aidong.util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
    public static String DATE_PATTERN_1        = "yyyy-MM-dd HH:mm:ss";
    public static String DATE_PATTERN_YYYYMMDD = "yyyy-MM-dd";
    public static String DATE_PATTERN_2        = "yyyyMMddHHmmss";

    public static Timestamp getCurrentTimestamp() {
        Timestamp ts = new Timestamp(Calendar.getInstance().getTimeInMillis());
        return ts;
    }

    public static String formatDate(Date date, String pattern) {
        DateFormat df = new SimpleDateFormat(pattern);
        return df.format(date);
    }

    public static Date formatDate(String date) {
        SimpleDateFormat df = new SimpleDateFormat(DATE_PATTERN_YYYYMMDD);
        Date tmpDate = null;
        try {
            tmpDate = df.parse(date);
        } catch (Exception e) {
            // 日期型字符串格式错误
        }
        return tmpDate;
    }

    public static Date formatDateByPattern(String date, String pattern) {
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        Date tmpDate = null;
        try {
            tmpDate = df.parse(date);
        } catch (Exception e) {
            // 日期型字符串格式错误
        }
        return tmpDate;
    }

    // 给定一个日期型字符串，返回加减n天后的日期型字符串
    public static String addDays(String basicDate, int n) {
        SimpleDateFormat df = new SimpleDateFormat(DATE_PATTERN_YYYYMMDD);
        Date tmpDate = null;
        try {
            tmpDate = df.parse(basicDate);
        } catch (Exception e) {
            // 日期型字符串格式错误
        }
        long nDay = (tmpDate.getTime() / (24 * 60 * 60 * 1000) + 1 + n) * (24 * 60 * 60 * 1000);
        tmpDate.setTime(nDay);

        return df.format(tmpDate);
    }

    // 给定一个日期，返回加减n天后的日期
    public static Date addDays(Date basicDate, int n) {

    	Date newDate= new Date();
        
    	long nDay = (basicDate.getTime() / (24 * 60 * 60 * 1000) + 1 + n) * (24 * 60 * 60 * 1000);
        
    	newDate.setTime(nDay);

        return newDate;
    }

    // 给定一个日期，返回加减n月后的日期
    public static Date addMonths(Date basicDate, int n) {

        Calendar rightNow = Calendar.getInstance();

        rightNow.setTime(basicDate);
        rightNow.add(Calendar.MONTH, n);
        Date dt = rightNow.getTime();
        return dt;
    }

    // 给定一个日期，返回加减n月后的日期
    public static Date addMonths(String basicDate, int n) {
        SimpleDateFormat df = new SimpleDateFormat(DATE_PATTERN_YYYYMMDD);
        Date tmpDate = null;
        try {
            tmpDate = df.parse(basicDate);
        } catch (Exception e) {
            // 日期型字符串格式错误
        }

        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(tmpDate);
        rightNow.add(Calendar.MONTH, n);
        Date dt = rightNow.getTime();
        return dt;
    }

    // 计算两个日期相隔的天数
    public static int nDaysBetweenTwoDate(Date firstDate, Date secondDate) {
        int nDay = (int) ((secondDate.getTime() - firstDate.getTime()) / (24 * 60 * 60 * 1000));
        return nDay;
    }

    // 计算两个日期相隔的天数
    public static int nDaysBetweenTwoDate(String firstString, String secondString) {
        SimpleDateFormat df = new SimpleDateFormat(DATE_PATTERN_YYYYMMDD);
        Date firstDate = null;
        Date secondDate = null;
        try {
            firstDate = df.parse(firstString);
            secondDate = df.parse(secondString);
        } catch (Exception e) {
            // 日期型字符串格式错误
        }

        int nDay = (int) ((secondDate.getTime() - firstDate.getTime()) / (24 * 60 * 60 * 1000));
        return nDay;
    }

    // 当月第一天
    public static String getFirstDay(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        return new SimpleDateFormat(DATE_PATTERN_YYYYMMDD).format(cal.getTime());

    }

    // 当月最后一天
    public static String getLastDay(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int value = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, value);
        return new SimpleDateFormat(DATE_PATTERN_YYYYMMDD).format(cal.getTime());
    }

    // 取日期的月份
    public static int getMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int value = cal.get(Calendar.MONTH) + 1;
        return value;
    }

}
